package com.elitetyc.customview.custom;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.elitetyc.customview.R;
import com.elitetyc.customview.adapter.DoubleCheckMonthAdpater;
import com.elitetyc.customview.model.DayBean;
import com.elitetyc.customview.model.MonthBean;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by elitetyc on 2019/6/22.
 * Describe:
 */
public class DoubleDateCheckView extends LinearLayout {

    private List<MonthBean> monthBeanList;

    private RecyclerView monthRv;
    private DoubleCheckMonthAdpater doubleCheckMonthAdpater;

    private DoubleCheckMonthAdpater.OnDateCheckLister onDateCheckLister;

    public void setOnDateCheckLister(DoubleCheckMonthAdpater.OnDateCheckLister onDateCheckLister) {
        this.onDateCheckLister = onDateCheckLister;
        doubleCheckMonthAdpater.setOnDateCheckLister(onDateCheckLister);
    }

    public DoubleDateCheckView(Context context) {
        this(context,null);
    }

    public DoubleDateCheckView(Context context,AttributeSet attrs) {
        this(context, attrs,0);
    }

    public DoubleDateCheckView(Context context,AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
        initViewData();
        initView(context);
    }

    private void initView(Context context) {
        View rootView = LayoutInflater.from(context).inflate(R.layout.view_double_date_check,null);
        monthRv = rootView.findViewById(R.id.rv_month);
        monthRv.setLayoutManager(new LinearLayoutManager(context));
        doubleCheckMonthAdpater = new DoubleCheckMonthAdpater(R.layout.view_month,monthBeanList);
        doubleCheckMonthAdpater.setmContext(context);
        monthRv.setAdapter(doubleCheckMonthAdpater);
        addView(rootView);


    }

    private void initViewData() {
        monthBeanList = new ArrayList<>();
        Calendar c = Calendar.getInstance();
        int nowDayOfMonth = c.get(Calendar.DAY_OF_MONTH);
        int nowMonth = c.get(Calendar.MONTH);
        int nowYear = c.get(Calendar.YEAR);
        c.add(Calendar.MONTH, -1);
//        初始化最近12个月的信息
        for (int i=0;i<=12;i++){
            c.add(Calendar.MONTH, 1);
            monthBeanList.add(new MonthBean(c.get(Calendar.YEAR),c.get(Calendar.MONTH) + 1));
        }
//      初始化天数信息
        for (MonthBean monthBean:monthBeanList){
            List<DayBean> dayBeanList = new ArrayList<>();
            Calendar calendar = Calendar.getInstance();
//            设置为这个月的第一天
            calendar.set(monthBean.year,monthBean.month-1,1);
            int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
            for (int i=1;i<dayOfWeek;i++){
                dayBeanList.add(new DayBean(monthBean.year,monthBean.month,0,false,0,false));
            }
            // 获取当月的天数
            int totalDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            for (int i=1;i<=totalDays;i++){
                if (i==1){
                    if (dayOfWeek==7){
//                        说明是第一天也是星期六
                        dayBeanList.add(new DayBean(monthBean.year,monthBean.month,i,false,2,true));
                    }else {
                        dayBeanList.add(new DayBean(monthBean.year,monthBean.month,i,false,1,true));
                    }
                }else if (i==totalDays){
                    Calendar calendar1 = Calendar.getInstance();
                    calendar1.set(monthBean.year,monthBean.month-1,i);
                    if (calendar1.get(Calendar.DAY_OF_WEEK)==1){
//                        说明是最后一天也是星期日
                        dayBeanList.add(new DayBean(monthBean.year,monthBean.month,i,false,2,true));
                    }else {
                        dayBeanList.add(new DayBean(monthBean.year,monthBean.month,i,false,3,true));
                    }

                }else {
                    Calendar calendartwo = Calendar.getInstance();
                    calendartwo.set(monthBean.year,monthBean.month-1,i);
                    if (calendartwo.get(Calendar.DAY_OF_WEEK)==7){
                        dayBeanList.add(new DayBean(monthBean.year,monthBean.month,i,false,3,true));
                    }else if (calendartwo.get(Calendar.DAY_OF_WEEK)==1){
                        dayBeanList.add(new DayBean(monthBean.year,monthBean.month,i,false,1,true));
                    }else
                    dayBeanList.add(new DayBean(monthBean.year,monthBean.month,i,false,4,true));
                }

            }
//                如果是当前月，就要设置当前月今天以前的日期不能选择
            if (calendar.get(Calendar.MONTH)==nowMonth&&calendar.get(Calendar.YEAR)==nowYear){
                for (int i=dayOfWeek-1;i<nowDayOfMonth+dayOfWeek-2;i++){
                    dayBeanList.get(i).canSelect = false;
                    if (i==dayOfWeek-1){
                        Calendar calendar1 = Calendar.getInstance();
                        calendar1.set(dayBeanList.get(i).year,dayBeanList.get(i).month-1,dayBeanList.get(i).day);
                        if (calendar1.get(Calendar.DAY_OF_WEEK)==7){
//                            说明是周六一个设置为圆
                            dayBeanList.get(i).lrm = 2;
                        }else {
                            dayBeanList.get(i).lrm = 1;
                        }
                    }

                    if (i==nowDayOfMonth+dayOfWeek-3){
                        Calendar calendar1 = Calendar.getInstance();
                        calendar1.set(dayBeanList.get(i).year,dayBeanList.get(i).month-1,dayBeanList.get(i).day);
                        if (calendar1.get(Calendar.DAY_OF_WEEK)==1){
//                            说明是周日一个设置为圆
                            dayBeanList.get(i).lrm = 2;
                        }else {
                            dayBeanList.get(i).lrm = 3;
                        }
                    }
                }
            }
            monthBean.dayBeanList = dayBeanList;
        }

    }

    private void init(){
        setOrientation(VERTICAL);
    }
}
